Expand description
§mq-markdown: Markdown parsing and manipulation for mq
This crate provides comprehensive markdown parsing, manipulation, and conversion functionality used in mq. It offers a robust API to work with markdown content and generate different output formats.
§Features
- Parse Markdown: Convert markdown strings to structured AST
- HTML Conversion: Convert between markdown and HTML formats
- MDX Support: Parse and manipulate MDX (Markdown + JSX) content
- JSON Export: Serialize markdown AST to JSON (with
jsonfeature) - Configurable Rendering: Customize output formatting and styles
- Built-in parser: CommonMark, GFM (tables, strikethrough, autolink literals, task lists, footnotes), frontmatter, math and MDX are parsed, and rendered to HTML, by a parser of this crate.
- Obsidian syntax: with the
wikilink,embedandcalloutfeatures,[[links]],![[embeds]]and> [!callouts]are parsed, and rendered to HTML with the classes Obsidian uses (internal-link,callout). Link targets are used as URLs as written.
§Quick Start
§Basic HTML Conversion
use mq_markdown::to_html;
let markdown = "# Hello, world!";
let html = to_html(markdown);
assert_eq!(html, "<h1>Hello, world!</h1>");§Working with Markdown AST
use mq_markdown::Markdown;
let markdown = "# Heading\n\nParagraph with *emphasis*";
let doc = markdown.parse::<Markdown>().unwrap();
println!("Found {} nodes", doc.nodes.len());
println!("HTML: {}", doc.to_html());
println!("Text: {}", doc.to_text());§Custom Rendering Options
use mq_markdown::{Markdown, RenderOptions, ListStyle};
let mut doc = "- Item 1\n- Item 2".parse::<Markdown>().unwrap();
doc.set_options(RenderOptions {
list_style: Some(ListStyle::Plus),
..Default::default()
});
// Now renders with "+" instead of "-"
println!("{}", doc);§Performance Considerations
- Use
&strmethods when possible to avoid unnecessary allocations - The AST uses structural equality checking for efficient comparisons
- Consider using
CompactStringfor memory-efficient string storage - Position information can be omitted to reduce memory usage
§HTML to Markdown (optional feature)
When the html-to-markdown feature is enabled, this crate can also convert HTML to Markdown.
ⓘ
// This example requires the `html-to-markdown` feature.
// Add `mq-markdown = { version = "...", features = ["html-to-markdown"] }` to your Cargo.toml.
use mq_markdown::convert_html_to_markdown;
let html = "<p>Hello <strong>world</strong>!</p>";
let markdown = convert_html_to_markdown(html)?;
assert_eq!(markdown, "Hello **world**!");Structs§
- Attr
Spec - An attribute a node kind has, with its type.
optionalattributes arenonewhen absent. - Blockquote
- A block quote.
- Break
- A hard line break.
- Code
- A code block.
- Code
Inline - Inline code.
- Color
Theme - Color theme for rendering markdown nodes with optional ANSI escape codes.
- Definition
- A link reference definition.
- Delete
- Strikethrough text.
- Emphasis
- Emphasized text.
- Footnote
- A footnote definition.
- Footnote
Ref - A footnote reference.
- Fragment
- A sequence of nodes with no wrapper.
- Heading
- A heading.
- Heading
Depth - The depth of a heading, from 1 to 6.
- Horizontal
Rule - A thematic break.
- Html
- Raw HTML.
- Image
- An inline image.
- Image
Ref - A reference-style image.
- Link
- An inline link.
- LinkRef
- A reference-style link.
- List
- A list item.
- Markdown
- A Markdown document.
- Math
- A math block.
- Math
Inline - Inline math.
- MdxDiagnostic
- An
MdxErrorwith the document, to show where in it the error is. - MdxError
- Invalid MDX, with where it is in the document when that is known.
- MdxFlow
Expression - A block-level MDX expression.
- MdxJs
Esm - MDX ESM
importorexport. - MdxJsx
Attribute - A property of a JSX element, with the value it has after its quotes or braces.
- MdxJsx
Flow Element - A block-level JSX element.
- MdxJsx
Text Element - An inline JSX element.
- MdxText
Expression - An inline MDX expression.
- Parse
Options - What reading Markdown or MDX turns on.
- Point
- A location in the source.
- Position
- A span of the source.
- Render
Options - Options for writing nodes as Markdown.
- Strong
- Strongly emphasized text.
- Table
Align - The delimiter row of a table, giving the column alignments.
- Table
Cell - A table cell.
- Table
Row - A table row.
- Text
- Plain text.
- Title
- A link title.
- Toml
- TOML front matter.
- Url
- A link destination.
- Yaml
- YAML front matter.
Enums§
- Attr
Type - The type of a node attribute as returned by
Node::attr. - Attr
Value - Represents a typed attribute value that can be returned from or passed to attr/set_attr methods.
- Horizontal
Rule Marker - The character a horizontal rule is written with in the source.
- List
Marker - The marker of a list item as it is written in the source.
- List
Style - Bullet marker used when writing lists.
- MdxAttribute
Content - An attribute of a JSX element: a property such as
a="b", or an expression such as{...props}. - MdxAttribute
Value - The value of a JSX property: a string, or an expression in braces.
- MdxError
Kind - What is wrong with invalid MDX.
- MdxFound
- What a JSX tag has where something else must be.
- MdxPlace
- The part of a JSX tag where an unexpected character is.
- Node
- A Markdown node.
- Node
Kind - The kind of a
Node. Headings are split by depth, so everyNodehas exactly one kind. - SetAttr
Error - The error of
Node::set_attr. - Table
Align Kind - The alignment of a table column.
- Title
Surround Style - How a link title is quoted when written.
- UrlSurround
Style - How a link destination is wrapped when written.
Functions§
- to_html
- Converts Markdown to HTML.